package com.neoCalf.neoCommon.redis;

import com.neoCalf.neoCommon.entity.UserInfo;
import com.neoCalf.neoCommon.enums.ResultCode;
import com.neoCalf.neoCommon.enums.UserStatusEnum;
import com.neoCalf.neoCommon.exception.BusinessException;
import com.neoCalf.neoCommon.utils.DBUtils;
import com.neoCalf.neoCommon.utils.DataTool;
import lombok.extern.slf4j.Slf4j;

import java.sql.SQLException;
import java.util.List;

import static com.neoCalf.neoCommon.constants.Constants.*;


@Slf4j
public class RedisComponent<T> {
    private final RedisTool<T> redisTool;

    public RedisComponent(RedisTool<T> redisTool) {
        this.redisTool = redisTool;
    }

    public void checkAccountStatus(String userId) {
        //账号状态校验
        //后台封禁账号时，一并修改持久层和缓存层，实时更新账号状态
        Integer status = (Integer) redisTool.getHash(REDIS_PREFIX + userId, "status");
        if (status == null) {
            String sql = "select * from user_info where user_id = ?";
            Object[] objects = new Object[]{userId};
            try {
                UserInfo userInfo = DBUtils.selectOne(sql, objects, UserInfo.class);
                if (userInfo == null) {
                    throw new BusinessException(ResultCode.CODE_10017, "账号不存在");
                }
                status = userInfo.getStatus();
                redisTool.putHash(REDIS_PREFIX + userId, "status", status);
            } catch (SQLException e) {
                log.error(String.valueOf(e));
                throw new BusinessException(ResultCode.SystemError);
            }
        }
        if (status.equals(UserStatusEnum.SHUT_DOWN.getType())) {
            throw new BusinessException(ResultCode.StatusHasInvalid);
        }
    }

    public void updateAccountStatus(String userId, UserStatusEnum statusEnum) {
        int status = statusEnum.getType();
        String sql = "update user_info set status = ? where user_id = ?";
        Object[] objects = new Object[]{status, userId};
        try {
            int result = DBUtils.update(sql, objects);
            DataTool.checkAffectRow(result, "系统出错了, 更改失败");
            redisTool.putHash(REDIS_PREFIX + userId, "status", status);
        } catch (SQLException e) {
            log.error(String.valueOf(e));
            throw new BusinessException(ResultCode.CODE_500, "系统出错了, 更改失败");
        }

    }

    public void delContactList(String userId) {
        redisTool.delete(userId + USER_CONTACT_LIST_KEY);
        redisTool.delete(userId + GROUP_LIST_KEY);
    }

    public void saveContactList(String userId, List<T> userContactList, List<T> groupList) {
        if (userContactList != null && !userContactList.isEmpty()) {
            redisTool.lPushAll(userId + USER_CONTACT_LIST_KEY, userContactList, TOKEN_EXPIRE);
        }
        if (groupList != null && !groupList.isEmpty()) {
            redisTool.lPushAll(userId + GROUP_LIST_KEY, groupList, TOKEN_EXPIRE);
        }
    }

    public void addContact(T userId, T contactId) {
        redisTool.lPush(userId + USER_CONTACT_LIST_KEY, contactId, TOKEN_EXPIRE);
    }

    public void addGroup(T userId, T groupId) {
        redisTool.lPush(userId + GROUP_LIST_KEY, groupId, TOKEN_EXPIRE);
    }

    public List<T> getContactList(T userId) {
        return redisTool.getQueuelist(userId + USER_CONTACT_LIST_KEY);
    }

    public List<T> getGroupList(T userId) {
        return redisTool.getQueuelist(userId + GROUP_LIST_KEY);
    }

    public List<T> getUserHeartBeatList() {
        return redisTool.getSet(REDIS_PREFIX_HEART_BEAT);
    }

    public void addUserHeartBeat(T userId) {
        redisTool.addSet(REDIS_PREFIX_HEART_BEAT, userId);
    }

    public void delUserHeartBeat(T userId) {
        redisTool.removeSet(REDIS_PREFIX_HEART_BEAT, userId);
    }
}
